﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using EquipmentConfig.Class;

namespace EquipmentConfig
{
    public partial class AllDebugInfo : Form
    {
        private MainForm mf;

        public AllDebugInfo(MainForm mainForm)
        {
            InitializeComponent();
            mf = mainForm;
            mf.recBuffer.Clear();
            rtbDebugMessage.Clear();
        }

        //public void BindData(string strMsg)
        //{
        //    if (strMsg != string.Empty)
        //    {
        //        string[] dataValue = strMsg.Replace("\r\n", "&").Split(new char[] { '&' });
        //        DataTable dt = new DataTable();
        //        dt.Columns.Add("Name");
        //        dt.Columns.Add("Value");

        //        for (int i = 0; i < dataValue.Length / 2; i++)
        //        {
        //            DataRow dr = dt.NewRow();
        //            dr[0] = dataValue[i * 2];
        //            dr[1] = dataValue[i * 2 + 1];
        //            dt.Rows.Add(dr);
        //        }
        //        dgvInfo.DataSource = dt;
        //    }
        //}

        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="msg">信息</param>
        public void ShowMessage(string msg)
        {
            rtbDebugMessage.Invoke((MethodInvoker)delegate
            {
                if (rtbDebugMessage.Lines.Length > 100)
                {
                    rtbDebugMessage.Clear();
                }
                rtbDebugMessage.AppendText(msg);//添加内容
                    rtbDebugMessage.Focus();
                rtbDebugMessage.ScrollToCaret();
            });
        }

        private void AllDebugInfo_Load(object sender, EventArgs e)
        {
            btnDebugRun_Click(null, null);
        }

        /// <summary>
        /// 开始调试
        /// </summary>
        private void btnDebugRun_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                MainForm.isSuccess = false;
                mf.SendCmd(Protocol.GetWriteDeviceCmd(0x09, new byte[2] { 0x01, 0x01 }));
                Thread sub = new Thread(() =>
                {
                    if (!mf.CheckReturnCmd())
                    {
                        Messages.ShowAlertDialog("开始调试操作失败！", false);
                    }
                    else
                    {
                        this.Invoke((MethodInvoker)delegate
                        {
                            btnDebugRun.Enabled = false;
                            btnDataUpload.Enabled = false;
                        });
                    }
                });
                sub.IsBackground = true;
                sub.Start();
                mf.timer2.Enabled = true;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("开始调试操作失败！", ex);
            }
        }

        /// <summary>
        /// 退出调试
        /// </summary>
        private void btnDebugExit_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                MainForm.isSuccess = false;
                mf.SendCmd(Protocol.GetWriteDeviceCmd(0x09, new byte[2] { 0x00, 0x00 }));
                //Thread sub = new Thread(() =>
                //{
                //    if (!mf.CheckReturnCmd())
                //    {
                //        Messages.ShowAlertDialog("退出调试操作失败！", false);
                //    }
                //});
                //sub.IsBackground = true;
                //sub.Start();
                mf.timer2.Enabled = true;
                btnDebugRun.Enabled = true;
                btnDataUpload.Enabled = true;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("退出调试操作失败！", ex);
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        private void AllDebugInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            mf.dataDebugInfo.Dispose();
            mf.dataDebugInfo = null;
            mf.timer2.Enabled = false;
            mf.recBuffer.Clear();
            mf.SendCmd(Protocol.GetWriteDeviceCmd(0x09, new byte[2] { 0x00, 0x00 }));
            Thread.Sleep(1000);
            mf.timer2.Enabled = true;
        }

        /// <summary>
        /// 清空
        /// </summary>
        private void btnDebugClear_Click(object sender, EventArgs e)
        {
            rtbDebugMessage.Clear();
        }

        /// <summary>
        /// 上报数据
        /// </summary>
        private void btnDataUpload_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                mf.SendCmd(Protocol.GetWriteDeviceCmd(0x08, new byte[1] { 0x01 }));
                mf.timer2.Enabled = true;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("上报数据失败！", ex);
            }
        }
    }
}
